package com.xuecheng.media.api;

import com.alibaba.nacos.common.http.param.MediaType;
import com.xuecheng.base.model.PageParams;
import com.xuecheng.base.model.PageResult;
import com.xuecheng.media.model.dto.UploadFilesParamDto;
import com.xuecheng.media.model.dto.UploadFilesResultDto;
import com.xuecheng.media.model.dto.QueryMediaParamsDto;
import com.xuecheng.media.model.po.MediaFiles;
import com.xuecheng.media.service.MediaFileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

/**
 * @description 媒资文件管理接口
 * @author Mr.M
 * @date 2022/9/6 11:29
 * @version 1.0
 */
 @Api(value = "媒资文件管理接口",tags = "媒资文件管理接口")
 @RestController
public class MediaFilesController {


  @Autowired
  MediaFileService mediaFileService;


 @ApiOperation("媒资列表查询接口")
 @PostMapping("/files")
 public PageResult<MediaFiles> list(PageParams pageParams, @RequestBody QueryMediaParamsDto queryMediaParamsDto){
  Long companyId = 1232141425L;
  return mediaFileService.queryMediaFiels(companyId,pageParams,queryMediaParamsDto);

 }
    @ApiOperation("媒资上传接口")
    @RequestMapping(value = "/upload/coursefile",consumes = MediaType.MULTIPART_FORM_DATA   )
    public UploadFilesResultDto upload(@RequestPart("filedata")MultipartFile filedata) throws IOException {
    //准备后端上传的数据
    UploadFilesParamDto uploadFilesParamDto = new UploadFilesParamDto();
    //文件名称
    uploadFilesParamDto.setFilename(filedata.getOriginalFilename());
    //文件大小
    uploadFilesParamDto.setFileSize(filedata.getSize());
    //文件类型，001001代表图片
    uploadFilesParamDto.setFileType("001001");
    //创建一个临时文件，把上传之后接收到的文件转换成新创建的临时文件，然后再拿到该文件的绝对路径
    File template = File.createTempFile("minio",".temp");
    filedata.transferTo(template);
    //拿到绝对文件路径
    String absolutePath = template.getAbsolutePath();
    Long companyId = 1232141425L;
    //开始上传图片
    UploadFilesResultDto uploadFilesResultDto = mediaFileService.FileUpload(companyId, uploadFilesParamDto, absolutePath);
    return uploadFilesResultDto;
    }
}
